<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>闭包的危害解决</title>
        <style>
            *{
                margin: 0;
                padding: 0;
            }
            ul{
                list-style: none;
            }
            li:nth-of-type(2n){
                background-color: red;
            }
            li:nth-of-type(2n+1){
                background-color: green;
            }
        </style>
</head>
<body>
    <ul>
        <li>a</li>
        <li>a</li>
        <li>a</li>
        <li>a</li>
        <li>a</li>
    </ul>
</body>
<script>
    function test() {
        var liCollection =document.getElementsByTagName('li');

        for (var i = 0; i < liCollection.length; i ++) {
            (function(j){
                liCollection[j].onclick = function () {
                    console.log(j)
                }
            }(i))
        }
    }

    test();
</script>
</html>
